<?php
/**
 * Created by PhpStorm.
 * User: mingzhongshui
 * Date: 2020/3/31
 * Time: 21:09
 */

namespace App\Repositories\Push;

use App\Repositories\Repository;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

/**
 * Class UserRepository
 * @package App\Repositories\User
 */
class PushRepository extends Repository
{

    /**
     * 任务生成状态
     */
    const UN_GENERATE_TASK = 0; // 未生成
    const GENERATE_TASK = 1; // 已生成

    /**
     * 状态
     */
    const STATUS_COMPLETED = 1; // 已完成
    const STATUS_UN_START = 0; // 未开始

    /**
     * @return string
     */
    public function model()
    {
        return \App\Models\Push\PushModel::class;
    }

    /**
     * @return mixed
     */
    public function getUnGenerateTaskPush()
    {
        return $this->model
                    ->where('is_generate_task', self::UN_GENERATE_TASK)
                    ->where('push_time', '<=', (new Carbon())->toDateTimeString())
                    ->get();
    }

    /**
     * 更新推送生成状态
     *
     * @param $pushId
     * @return mixed
     */
    public function updateGenerateStatus($pushId)
    {
        return $this->model
                    ->where('id', $pushId)
                    ->update([
                        'is_generate_task' => self::GENERATE_TASK
                    ]);
    }

    /**
     * @param $pushId
     * @param $count
     * @return mixed
     */
    public function updateCompleteStatus($pushId, $count)
    {
        return $this->model
                    ->where('id', $pushId)
                    ->update([
                        'success' => $count,
                        'complete_time' => (new Carbon())->toDateTimeString(),
                        'status' => self::STATUS_COMPLETED
                    ]);
    }

}
